<%@page import="java.util.List"%>
<%@page import="coba.service.ProvinciaLocalServiceUtil"%>
<%@page import="coba.service.InstitucionLocalServiceUtil"%>
<%@page import="coba.service.ContactoLocalServiceUtil"%>
<%@page import="coba.service.RecorridoLocalServiceUtil"%>
<%@page import="coba.model.Provincia"%>
<%@page import="coba.model.Institucion"%>
<%@page import="coba.model.Contacto"%>
<%@page import="coba.model.Recorrido"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />

<%
	PortletURL urlList = renderResponse.createRenderURL();
	urlList.setParameter("jspPage","/views/recorrido/list.jsp");

	PortletURL urlRecorridoForm = renderResponse.createActionURL();
	urlRecorridoForm.setParameter(ActionRequest.ACTION_NAME, "recorridoForm");
	urlRecorridoForm.setParameter("redirectTo", urlList.toString());
	
	Long recorridoId = renderRequest.getParameter("id") != null &&
			!renderRequest.getParameter("id").trim().equals("") ?
			Long.valueOf(renderRequest.getParameter("id")) :
			null;

	Recorrido recorrido = recorridoId != null ?
			RecorridoLocalServiceUtil.getRecorrido(recorridoId) :
			null;
%>

<script>
	function validateEmail(email) {
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		return re.test(email)
	}

	function validateEmails(emails) {
		if (emails.indexOf(',') > -1) {
			var arrEmails = emails.split(',')
			var hayError = false
			
			arrEmails.forEach(function(mail) {
				if (!validateEmail(mail))
					hayError = true
			})

			return !hayError
		}
		else {
			return validateEmail(emails)
		}		
	}

	function aceptar() {
		if (validateEmails($('#mails').val())) {
			return true
		}
		else {
			alert('Alguno de los emails ingresados no es valido o no esta separado por coma.')
			return false
		}
	}
</script>

<div>
	<div class="taglib-header">
		<span class="header-back-to">
			<a class="icon-circle-arrow-left previous-level" href="<%=urlList%>" title="Atr&aacute;s">
				<span class="helper-hidden-accessible"> Atr&aacute;s </span>
			</a>
		</span>
		<h3 class="header-title"><span> Atractivo </span></h3>
	</div>

	<liferay-ui:error key="recorridoExistente" message="Ya existe un atractivo con ese nombre." />

	<div>
		<form action="<%=urlRecorridoForm %>" method="post">
			<input type="hidden" name="<portlet:namespace />id" value="<%=recorrido != null ? recorrido.getId() : ""%>" />

			<div class="form-group">
				<label for="<portlet:namespace />nombre">Nombre</label>
				<input required maxlength="50" type="text" class="form-control campo" name="<portlet:namespace />nombre" id="nombre" value="<%=recorrido != null ? recorrido.getNombre() : ""%>">
			</div>
			
			<div class="form-group">
				<label for="<portlet:namespace />capacidad">Capacidad</label>
				<input required min="1" max="99999" type="number" class="form-control campo" name="<portlet:namespace />capacidad" id="capacidad" value="<%=recorrido != null ? recorrido.getCapacidad() : ""%>">
			</div>

			<div class="form-group">
				<label for="<portlet:namespace />mails">Mails</label>
				<input required maxlength="50" type="text" class="form-control campo" name="<portlet:namespace />mails" id="mails" value="<%=recorrido != null ? recorrido.getMails() : ""%>">
			</div>

			<div>
				<button type="submit" class="btn btn-primary" onclick="return aceptar()">Aceptar</button>
			</div>
		</form>
	</div>	
</div>
